import {pushTarget,popTarget} from "./Dep.js"
export default class Watcher{
    constructor(data,exp,cb){
        this.data = data;
        this.expression = exp;
        this.callback = cb;
        this.value = this.get();
    }
    get(){
        pushTarget(this);
        const value = parsePath(this.data,this.expression);
        popTarget();
        return value;
    }
    update(){
        const oldvalue = this.value;
        this.value = parsePath(this.data,this.expression);
        this.callback.call(this.data, this.value , oldvalue)
    }
}
function parsePath(obj,expression){
    const str = expression.split(".");
    for(let key of str){
        if (!obj) return
        obj = obj[key]
    }
    return obj
}